

package com.magicking.apnswitch;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;

/**
 * @author Pavlov Dmitry <pavlov.dmitry.n@gmail.com>
 */
public class StatusReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION_ID = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ApplicationConstants.STATUS_CHANGED_MESSAGE.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            if (extras != null){
                //let's save current state in preferences
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                final boolean isNetEnabled = extras.getBoolean(ApplicationConstants.STATUS_EXTRA);
                prefs.edit().putBoolean(ApplicationConstants.SETTINGS_TOGGLE_BUTTON, isNetEnabled).commit();

                //check if we should show notification
//                if (extras.getBoolean(ApplicationConstants.SHOW_NOTIFICATION, true)) {
//                    performNotificationStatusChange(context, isNetEnabled);
//                }
                int notifySummaryId = isNetEnabled ? R.string.status_enabled : R.string.status_disabled;
                String notifySummaryText = context.getResources().getString(notifySummaryId);
                Toast.makeText(context, notifySummaryText, Toast.LENGTH_LONG).show();
            }
        }
    }

//    private void performNotificationStatusChange(Context context, boolean isNetEnabled) {
//        int iconId = isNetEnabled ? R.drawable.stat_apnswitcher_on : R.drawable.stat_apnswitcher_off;
//        // display notification icon without text
//        Notification notification = new Notification(iconId, null, System.currentTimeMillis());
//
//        Intent intent = new Intent(context, InfoActivity.class);
//        intent.putExtra(InfoActivity.EXTRA_IS_NET_ENABLED, isNetEnabled);
//
//        PendingIntent pendingIntent = PendingIntent.getActivity(context, -1 /* not used in SDK1.0 */, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//
//        String notifyTitle = context.getResources().getString(R.string.app_name);
//        int notifySummaryId = isNetEnabled ? R.string.status_enabled : R.string.status_disabled;
//        String notifySummaryText = context.getResources().getString(notifySummaryId);
//        notification.setLatestEventInfo(context, notifyTitle, notifySummaryText, pendingIntent);
//
//        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//        notificationManager.notify(NOTIFICATION_ID, notification);
//    }

}
